In Java, methods can return arrays. This is useful when you want to perform operations on a group of elements and then return them for further use.
The general syntax of a method that returns an array is as follows:
Syntax
general syntax
dataType[] methodName() {
// statements;
// return arrayName;
}
Here, `dataType` represents the type of array being returned. The use of one pair of brackets (`[]`) indicates that the method is returning a one-dimensional array of type `dataType`.
Here's an example of a method that returns an array:
Basic example of returning array
example of a method that returns an array in java
public class Main {
public static void main(String[] args) {
int[] returnedArray = getArray();
for (int num : returnedArray) {
System.out.println(num);
}
}
public static int[] getArray() {
int[] array = {1, 2, 3, 4, 5};
return array;
}
}
Output
1
2
3
4
5
In this example, the `getArray` method returns an array of integers. We call this method in the `main` method and store the returned array in the `returnedArray` variable. We then print each element in the returned array.
You can also return multidimensional arrays from methods. Here's an example:
Returning multidimensional arrays
Returning multidimensional arrays from methods in java
public class Main {
public static void main(String[] args) {
int[][] returnedArray = getArray();
for (int i = 0; i < returnedArray.length; i++) {
for (int j = 0; j < returnedArray[i].length; j++) {
System.out.print(returnedArray[i][j] + " ");
}
System.out.println();
}
}
public static int[][] getArray() {
int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
return array;
}
}
Output
1 2 3
4 5 6
7 8 9
In this example, the `getArray` method returns a two-dimensional array of integers. We call this method in the `main` method and store the returned array in the `returnedArray` variable. We then print each element in the returned array.
here are some examples of methods returning arrays of different data types in Java:
Returning an array of doubles
Returning an array of doubles from method in java
public class Main {
public static void main(String[] args) {
double[] returnedArray = getArray();
for (double num : returnedArray) {
System.out.println(num);
}
}
public static double[] getArray() {
double[] array = {1.1, 2.2, 3.3, 4.4, 5.5};
return array;
}
}
Output
1.1
2.2
3.3
4.4
5.5
Returning an array of Strings
Returning an array of Strings from method in java
public class Main {
public static void main(String[] args) {
String[] returnedArray = getArray();
for (String str : returnedArray) {
System.out.println(str);
}
}
public static String[] getArray() {
String[] array = {"Java", "Python", "C++", "JavaScript", "Ruby"};
return array;
}
}
Output
Java
Python
C++
JavaScript
Ruby
Returning an array of custom objects
Returning an array of custom objects in java
public class Main {
public static void main(String[] args) {
Student[] returnedArray = getArray();
for (Student student : returnedArray) {
System.out.println(student.name);
}
}
public static Student[] getArray() {
Student[] array = {new Student("John"), new Student("Jane"), new Student("Doe")};
return array;
}
}
class Student {
String name;
Student(String name) {
this.name = name;
}
}
Output
John
Jane
Doe
In each of these examples, the `getArray` method returns an array of a specific data type. We call this method in the `main` method and store the returned array in the `returnedArray` variable. We then print each element in the returned array.
when a method returns an array, it's actually returning a reference to that array. This means that any changes made to the returned array will affect the original array.